package com.smalls.redshoes.ui.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.smalls.newvideotwo.NewDTVApp;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.redshoes.util.ViewUtils;

import org.apache.http.HttpStatus;

public class GrapeGridview extends BaseGridView {
    private int LastNumber;
    private int NumberLine;
    private static String TAG = Constant.baddcoolio2tag?Constant.TAGPREFIXSTR + GrapeGridview.class.getSimpleName():GrapeGridview.class.getSimpleName();
    private Runnable downRunable = new Runnable() {
        public void run() {
            if (GrapeGridview.this.getSelectedItemPosition() + (GrapeGridview.this.mRow * GrapeGridview.this.getNumColumns()) < GrapeGridview.this.getCount()) {
                GrapeGridview.this.setSelection(GrapeGridview.this.getSelectedItemPosition() + (GrapeGridview.this.mRow * GrapeGridview.this.getNumColumns()));
            }
            GrapeGridview.this.setFillingFalse();
        }
    };
    private int halfHeight;
    private boolean isDownfilling = false;
    private boolean isUpfilling = false;
    private int mDuration = 800;
    private int mGridViewHeight = 610;
    private int mRow = 2;
    private int mSelectedDuration = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    private Runnable pageRunable = new Runnable() {
        public void run() {
            if (GrapeGridview.this.getSelectedItemPosition() + (GrapeGridview.this.mRow * GrapeGridview.this.getNumColumns()) < GrapeGridview.this.getCount()) {
                GrapeGridview.this.setSelection(GrapeGridview.this.getSelectedItemPosition() + (GrapeGridview.this.mRow * GrapeGridview.this.getNumColumns()));
            }
        }
    };
    private Runnable upRunable = new Runnable() {
        public void run() {
            if (GrapeGridview.this.getSelectedItemPosition() - (GrapeGridview.this.mRow * GrapeGridview.this.getNumColumns()) >= 0) {
                GrapeGridview.this.setSelection(GrapeGridview.this.getSelectedItemPosition() - (GrapeGridview.this.mRow * GrapeGridview.this.getNumColumns()));
            }
            GrapeGridview.this.setFillingFalse();
        }
    };
    private int verHeight = 10;

    public GrapeGridview(Context context) {
        super(context);
        this.verHeight = ViewUtils.DipToPixels(context, 0.0f);
    }

    public GrapeGridview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.verHeight = ViewUtils.DipToPixels(context, 0.0f);
    }

    public GrapeGridview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.verHeight = ViewUtils.DipToPixels(context, 0.0f);
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == 19) {
            if (event.getAction() != 0 || this.isUpfilling || this.isDownfilling) {
                return true;
            }
            setFillingTure();
            if (getItemCurrentRow() || getSelectedItemPosition() <= (this.mRow * getNumColumns()) - 1) {
                setFillingFalse();
                return super.dispatchKeyEvent(event);
            }
            smoothScrollBy((-this.mGridViewHeight) + this.verHeight, this.mDuration);
            postDelayed(this.upRunable, (long) this.mSelectedDuration);
            return true;
        } else if (event.getKeyCode() != 20) {
            if (event.getKeyCode() == 21) {
                if (getSelectedItemPosition() % getNumColumns() == 0) {
                    return false;
                }
            } else if (event.getKeyCode() == 22) {
                if (event.getAction() != 0) {
                    return true;
                }
                if (getSelectedItemPosition() % getNumColumns() == getNumColumns() - 1) {
                    if (!getItemCurrentRow()) {
                        this.LastNumber = getCount() % getNumColumns() == 0 ? getNumColumns() : getCount() % getNumColumns();
                        this.NumberLine = (getSelectedItemPosition() / getNumColumns()) + 1;
                        if (getSelectedItemPosition() >= (getNumColumns() * this.NumberLine) - 1) {
                            if (getSelectedItemPosition() >= (getCount() - 1) - this.LastNumber) {
                                if (getSelectedItemPosition() + 1 < getCount()) {
                                    setSelection(getSelectedItemPosition() + 1);
                                }
                            } else if (getSelectedItemPosition() < getCount() - 1 && getSelectedItemPosition() + 1 < getCount()) {
                                setSelection(getSelectedItemPosition() + 1);
                            }
                        }
                    } else if (getItemCurrentRow()) {
                        this.LastNumber = getCount() % getNumColumns() == 0 ? getNumColumns() : getCount() % getNumColumns();
                        this.NumberLine = (getSelectedItemPosition() / getNumColumns()) + 1;
                        if (getSelectedItemPosition() >= (getNumColumns() * this.NumberLine) - 1) {
                            if (getSelectedItemPosition() >= (getCount() - 1) - this.LastNumber) {
                                if (getSelectedItemPosition() + 1 < getCount()) {
                                    setSelection(getSelectedItemPosition() + 1);
                                }
                            } else if (getSelectedItemPosition() + (this.mRow * getNumColumns()) <= getCount() - 1) {
                                smoothScrollBy(this.mGridViewHeight - this.verHeight, this.mDuration);
                                postDelayed(this.pageRunable, (long) this.mSelectedDuration);
                            }
                        }
                    }
                }
            }
            return super.dispatchKeyEvent(event);
        } else if (event.getAction() != 0 || this.isUpfilling || this.isDownfilling) {
            return true;
        } else {
            setFillingTure();
            if (!getItemCurrentRow() || getSelectedItemPosition() >= getCount() - (this.mRow * getNumColumns())) {
                setFillingFalse();
                return super.dispatchKeyEvent(event);
            }
            smoothScrollBy(this.mGridViewHeight - this.verHeight, this.mDuration);
            postDelayed(this.downRunable, (long) this.mSelectedDuration);
            return true;
        }
    }

    private void setFillingTure() {
        this.isUpfilling = true;
        this.isDownfilling = true;
    }

    private void setFillingFalse() {
        this.isUpfilling = false;
        this.isDownfilling = false;
    }

    public boolean getItemCurrentRow() {
        if (getSelectedView() == null || getSelectedView().getBottom() <= getHalfHeigh()) {
            return false;
        }
        return true;
    }

    private int getHalfHeigh() {
        if (this.halfHeight > 0) {
            return this.halfHeight;
        }
        WindowManager manager = (WindowManager) NewDTVApp.getApp().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(dm);
        this.halfHeight = dm.heightPixels / 2;
        return this.halfHeight;
    }
}
